using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;

namespace Sailfish
{
    public class JsonValueSingle : JsonGUIValue
    {
        string tempStr;
        string valueStr;

        string pattern = @"^[0-9.]+$";

        public JsonValueSingle(Type type, object value, string name) : base(type, value, name)
        {
            tempStr = valueStr =$"{value}";
        }


        public override void CustomGUI()
        {
            if (type == typeof(float))
            {
                Field((float)value, name);
            }
            else if (type == typeof(int))
            {
                Field((int)value, name);
            }
            else
            {
                NoTypeField(type, name);
            }
        }


        void Field<T>(T value, string name)
        {
            GUILayout.BeginHorizontal();
            TitleLayout();

            tempStr = GUILayout.TextField(tempStr);
            GUILayout.EndHorizontal();

            if (valueStr != tempStr)
            {
                var match = Regex.Match(tempStr, pattern);
                if (match.Success)
                {
                    valueStr = match.Value;
                    this.value = (T)Convert.ChangeType(valueStr, typeof(T));
                }
                else
                    tempStr = valueStr;
            }
        }
    }
}
